package com.addcn.android.design591.fragment;

import android.graphics.Region;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.addcn.android.design591.R;
import com.addcn.android.design591.adapter.SpinnerAreaAdapter;
import com.addcn.android.design591.adapter.SpinnerCityAdatper;
import com.addcn.android.design591.bean.CityAreaBean;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * Created by xuge on 2016/12/30.
 * 设计报价
 */

public class OfferFragment extends Fragment implements View.OnClickListener {

    private EditText offer_edit_name;
    private EditText offer_edit_phone;
    private Spinner offer_spinner_city;
    private Spinner offer_spinner_area;
    private EditText offer_edit_code;
    private Button offer_button_send_code;
    private Button offer_button_send;

    private SpinnerCityAdatper spinnerCityAdatper = null;
    private SpinnerAreaAdapter spinnerAreaAdapter = null;

    private ArrayList<String> arrayList_city = new ArrayList<>();
    private ArrayList<String> arrayList_area = new ArrayList<>();

    private CityAreaBean cityAreaBean = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_offer, null);
        initView(contentView);
        return contentView;
    }


    private void initView(View contentView) {
        offer_edit_name = (EditText) contentView.findViewById(R.id.offer_edit_name);
        offer_edit_phone = (EditText) contentView.findViewById(R.id.offer_edit_phone);
        offer_spinner_city = (Spinner) contentView.findViewById(R.id.offer_spinner_city);
        offer_spinner_area = (Spinner) contentView.findViewById(R.id.offer_spinner_area);
        offer_edit_code = (EditText) contentView.findViewById(R.id.offer_edit_code);
        offer_button_send_code = (Button) contentView.findViewById(R.id.offer_button_send_code);
        offer_button_send = (Button) contentView.findViewById(R.id.offer_button_send);

        offer_button_send_code.setOnClickListener(this);
        offer_button_send.setOnClickListener(this);


        String address = getString(R.string.address);
        cityAreaBean = new Gson().fromJson(address, CityAreaBean.class);

        int region_size = cityAreaBean.data.region.size();
        for (int i = 0; i < region_size; i++) {
            CityAreaBean.DataBean.RegionBean regionBean = cityAreaBean.data.region.get(i);
            arrayList_city.add(regionBean.name);

            if (i == 0) {
                int select_size = regionBean.section.size();
                for (int j = 0; j < select_size; j++) {
                    arrayList_area.add(regionBean.section.get(j).text);
                }
            }
        }


        spinnerCityAdatper = new SpinnerCityAdatper(getContext(), R.layout.spinner_item, arrayList_city);
        spinnerAreaAdapter = new SpinnerAreaAdapter(getActivity(), R.layout.spinner_item, arrayList_area);


        offer_spinner_city.setAdapter(spinnerCityAdatper);
        offer_spinner_area.setAdapter(spinnerAreaAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.offer_button_send_code:

                break;
            case R.id.offer_button_send:

                break;
        }
    }

    private void submit() {
        // validate
        String name = offer_edit_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(getContext(), "输入您的姓名", Toast.LENGTH_SHORT).show();
            return;
        }

        String phone = offer_edit_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getContext(), "输入您的电话号码", Toast.LENGTH_SHORT).show();
            return;
        }

        String code = offer_edit_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(getContext(), "手机确认码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
